<script setup lang="ts">
import { createConsultOrder, getConsultOrderPayUrl, getConsultOrderPre } from '@/services/consult';
import { getPatientDetail } from '@/services/user';
import { useConsultStore } from '@/stores/modules/consult';
import type { ConsultOrderPreData } from '@/types/consult';
import type { Patient } from '@/types/user';
import { showLoadingToast, showNotify } from 'vant';
import { onMounted, ref } from 'vue';

const store = useConsultStore()

// 定义两个数据
const payInfo = ref<ConsultOrderPreData>() // 支付信息
const patient = ref<Patient>() // 患者信息
// 封装两个调用函数
const loadData = async () => {
  const res = await getConsultOrderPre({
    type: store.consult.type, // 极速问诊
    illnessType: store.consult.illnessType // 三甲
  })
  payInfo.value = res.data
  // 设置默认优惠券
  store.setCouponId(payInfo.value.couponId) // 优惠券ID存下来

}
const loadPatient = async () => {
  if (!store.consult.patientId) return
  const res = await getPatientDetail(store.consult.patientId)
  patient.value = res.data
}

// onMounted调用两个函数即可
onMounted(() => {
  loadData()
  loadPatient()
})


const show = ref(false)
// 0 微信 1 支付宝
const paymentMethod = ref<0|1>(1)

// 订单id
const orderId = ref('')
// loading
const loading = ref(false)

const submit = async () => {
  if (!isAgree.value) {
    showNotify('请同意协议')
    return
  }
  loading.value = true
  // 先生成订单 再选择支付
  const res = await createConsultOrder(store.consult)
  orderId.value = res.data.id

  store.clear() // 清空仓库数据  当前的仓库数据被下单了 也就没用了
  loading.value = false




  // 打开抽屉 选择支付方式
  show.value = true
}

// 是否同意
const isAgree = ref(true)


const pay = async () => {
  // 支付方式的判断
  if (paymentMethod.value === 0) {
    // 微信支付
    showNotify('微信支付暂不支持')
    return
  }

  // loading提示
  showLoadingToast({
    message: '正在跳转支付...',
    duration: 0 // 不会自动关闭
  })

  // 支付
  const res = await getConsultOrderPayUrl({
    paymentMethod: 1,
    orderId: orderId.value,
    payCallback: 'http://localhost:5173/room'
  })  // http://localhost:5173/room    ?orderId=7201807023988736&payResult=true&type=2
  // router.push('/room')  跳转网站内部
  window.location.href = res.data.payUrl // 支付链接（外部链接）
  // 后续支付宝接管支付过程 =》
}
</script>

<template>
  <div class="consult-pay-page">
    <cp-nav-bar title="支付" />
    <div class="pay-info">
      <p class="tit">图文问诊 {{ payInfo?.payment }} 元</p>
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p class="desc">
        <span>极速问诊</span>
        <span>自动分配医生</span>
      </p>
    </div>
    <van-cell-group>
      <van-cell title="优惠券" :value="`-¥${payInfo?.couponDeduction}`" />
      <van-cell title="积分抵扣" :value="`-¥${payInfo?.pointDeduction}`" />
      <van-cell title="实付款" :value="`¥${payInfo?.actualPayment}`" class="pay-price" />
    </van-cell-group>
    <div class="pay-space"></div>
    <van-cell-group>
      <van-cell title="患者信息" :value="`${patient?.name} | ${patient?.genderValue} | ${patient?.age}岁`"></van-cell>
      <van-cell title="病情描述" :label="store.consult.illnessDesc"></van-cell>
    </van-cell-group>
    <div class="pay-schema">
      <van-checkbox v-model="isAgree">我已同意 <span class="text">支付协议</span></van-checkbox>
    </div>
    <!-- 这里不是直接支付 -->
    <!-- 需要先生成订单  利用支付宝为订单支付 -->
    <van-submit-bar :loading="loading" @click="submit" button-type="primary" :price="payInfo?.actualPayment ? payInfo?.actualPayment * 100 : 0" button-text="立即支付" text-align="left" />


    <van-action-sheet v-model:show="show" title="选择支付方式">
      <div class="pay-type">
        <p class="amount">￥{{ payInfo?.actualPayment.toFixed(2) }}</p>
        <van-cell-group>
          <van-cell title="微信支付" @click="paymentMethod = 0">
            <template #icon><cp-icon name="consult-wechat" /></template>
            <template #extra><van-checkbox :checked="paymentMethod === 0" /></template>
          </van-cell>
          <van-cell title="支付宝支付" @click="paymentMethod = 1">
            <template #icon><cp-icon name="consult-alipay" /></template>
            <template #extra><van-checkbox :checked="paymentMethod === 1" /></template>
          </van-cell>
        </van-cell-group>
        <div class="btn">
          <van-button @click="pay" type="primary" round block>立即支付</van-button>
        </div>
      </div>
    </van-action-sheet>
  </div>
</template>

<style lang="scss" scoped>
.consult-pay-page {
  padding: 46px 0 50px 0;
}
.pay-info {
  display: flex;
  padding: 15px;
  flex-wrap: wrap;
  align-items: center;
  .tit {
    width: 100%;
    font-size: 16px;
    margin-bottom: 10px;
  }
  .img {
    margin-right: 10px;
    width: 38px;
    height: 38px;
    border-radius: 4px;
    overflow: hidden;
  }
  .desc {
    flex: 1;
    > span {
      display: block;
      color: var(--cp-tag);
      &:first-child {
        font-size: 16px;
        color: var(--cp-text2);
      }
    }
  }
}
.pay-price {
  ::v-deep() {
    .vam-cell__title {
      font-size: 16px;
    }
    .van-cell__value {
      font-size: 16px;
      color: var(--cp-price);
    }
  }
}
.pay-space {
  height: 12px;
  background-color: var(--cp-bg);
}
.pay-schema {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  .text {
    color: var(--cp-primary);
  }
}
::v-deep() {
  .van-submit-bar__button {
    font-weight: normal;
    width: 160px;
  }
}
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }
  .btn {
    padding: 15px;
  }
  .van-cell {
    align-items: center;
    .cp-icon {
      margin-right: 10px;
      font-size: 18px;
    }
    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
</style>
